/**
 * 
 */
package org.mspring.mlog.schedule.job;

import java.io.File;
import java.util.Date;

import org.mspring.mlog.core.ServiceFactory;
import org.mspring.mlog.service.OptionService;
import org.mspring.mlog.service.PostService;
import org.mspring.mlog.utils.WebUtils;
import org.mspring.platform.api.sitemap.ChangeFreq;
import org.mspring.platform.api.sitemap.GoogleCodeSitemapGenerator;
import org.mspring.platform.api.sitemap.GoogleCodeSitemapUrl;
import org.mspring.platform.api.sitemap.GoogleCodeSitemapUrl.FileType;
import org.mspring.platform.api.sitemap.WebSitemapGenerator;
import org.mspring.platform.api.sitemap.WebSitemapUrl;
import org.quartz.JobExecutionContext;
import org.springframework.stereotype.Service;

/**
 * @author Gao Youbo
 * @since 2013-1-25
 * @description
 * @TODO
 */
@Service
public class NewsSitemapJob extends BaseJob {

    private OptionService optionService = ServiceFactory.getOptionService();

    @Override
    public void nativeExecuteInternal(JobExecutionContext context) {
        // TODO Auto-generated method stub
        try {
            String baseUrl = optionService.getOption("siteurl");
            File baseDir = new File(WebUtils.getRealContextPath());

            WebSitemapGenerator generator = WebSitemapGenerator.builder(baseUrl, baseDir).fileNamePrefix("sitemap_news").build();
            GoogleCodeSitemapGenerator googleGenerator = GoogleCodeSitemapGenerator.builder(baseUrl, baseDir).fileNamePrefix("sitemap_news_google").build();

            for (int i = 0; i < 100000; i++) {
                String loc = "http://www.javaos.org/news/322"; // URL
                Date lastMod = new Date(); // 最后修改
                double priority = 1.0; // 优先级
                WebSitemapUrl url = new WebSitemapUrl.Options(loc).lastMod(lastMod).priority(priority).changeFreq(ChangeFreq.DAILY).build();
                generator.addUrl(url);

                // google
                GoogleCodeSitemapUrl googleUrl = new GoogleCodeSitemapUrl.Options(loc, FileType.HTML).lastMod(lastMod).priority(priority).changeFreq(ChangeFreq.DAILY).build();
                googleGenerator.addUrl(googleUrl);
            }
            generator.write();
            googleGenerator.write();
        } catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
    }
}
